#!/bin/sh
#packet
rootPath=../install/$1
rootPedPath=../install/$1/ped
binPath=${rootPedPath}/bin
etcPath=${rootPedPath}/etc
libPath=${rootPedPath}/lib
confPath=${rootPedPath}/conf
modulePath=${rootPedPath}/module
servicePath=${etcPath}/systemd/system
ldconfigFilePath=${etcPath}/ld.so.conf.d

#install path
# installRootPath=/usr/local
# installPedRootPath=${installRootPath}/ped
# installPedBinPath=${installPedRootPath}/bin
# installPedConfPath=${installPedRootPath}/conf
# installPedDataPath=${installPedRootPath}/data
# installPedLibPath=${installPedRootPath}/lib
# installPedModulePath=${installPedRootPath}/module
# #upgrade path
# installUpgradeRootPath=${installRootPath}/upgrade
# installUpgradeConfPath=${installUpgradeRootPath}/conf
# installUpgradeBinPath=${installUpgradeRootPath}/bin
# installUpgradeLibPath=${installUpgradeRootPath}/lib

thirdPartyPath=${rootPath}/third_party

echo "begin to create installation package..."
#packet path
if [ ! -d ${binPath} ]; then
    mkdir -p ${binPath}
fi
if [ ! -d ${confPath} ]; then
    mkdir -p ${confPath}
fi
if [ ! -d ${libPath} ]; then
    mkdir -p ${libPath}
fi
if [ ! -d ${modulePath} ]; then
    mkdir -p ${modulePath}
fi
if [ ! -d ${servicePath} ]; then
    mkdir -p ${servicePath}
fi
if [ ! -d ${etcPath} ]; then
    mkdir -p ${etcPath}
fi
if [ ! -d ${ldconfigFilePath} ]; then
    mkdir -p ${ldconfigFilePath}
fi
#ped
# if [ ! -d ${installPedBinPath} ]; then
#     mkdir -p ${installPedBinPath}
# fi
# if [ ! -d ${installPedConfPath} ]; then
#     mkdir -p ${installPedConfPath}
# fi
# if [ ! -d ${installPedDataPath} ]; then
#     mkdir -p ${installPedDataPath}
# fi
# if [ ! -d ${installPedLibPath} ]; then
#     mkdir -p ${installPedLibPath}
# fi
# if [ ! -d ${installPedModulePath} ]; then
#     mkdir -p ${installPedModulePath}
# fi
# #upgrade
# if [ ! -d ${installUpgradeConfPath} ]; then
#     mkdir -p ${installUpgradeConfPath}
# fi
# if [ ! -d ${installUpgradeBinPath} ]; then
#     mkdir -p ${installUpgradeBinPath}
# fi
# if [ ! -d ${installUpgradeLibPath} ]; then
#     mkdir -p ${installUpgradeLibPath}
# fi


echo "begin to delete last packet..."
rm -rf ../install/ped_$2_$1.tar.gz

echo "coping so file and ped..."
cp -rpf ../target/$1/bin/ped ${binPath}/
cp -rpf ../target/$1/bin/upgrade ${binPath}/
cp -rpf ../lib/$1/* ${libPath}/


cp -rpf ../target/$1/lib/idgen.so ${libPath}/
cp -rpf ../target/$1/lib/libcjson.so ${libPath}/
cp -rpf ../target/$1/lib/libdb.so ${libPath}/
cp -rpf ../target/$1/lib/libEasyLogger.so ${libPath}/
cp -rpf ../target/$1/lib/libidgen.so ${libPath}/
cp -rpf ../target/$1/lib/libIdHelper.so ${libPath}/
cp -rpf ../target/$1/lib/libsettings.so ${libPath}/
if [ -f ../target/$1/lib/libuv.so ]; then
    cp -rpf ../target/$1/lib/libuv.so ${libPath}/
else
    cp -rpf ../target/$1/lib/libuv.so.1.0.0 ${libPath}/
fi

#upgrade
#cp -rpf ../lib/aarch32/liblogger.so.1.0.0 ${pedLibPath}/
#cp -rpf ../lib/aarch32/libsettings.so.1.0.0 ${pedLibPath}/
#cp -rpf ../lib/aarch32/libsysconfig.so ${pedLibPath}/
#cp -rpf ../lib/aarch32/libsysinfo.so ${pedLibPath}/
#cp -rpf ../lib/aarch32/libcurl.so ${pedLibPath}/
#cp -rpf ../lib/aarch32/libssl.so ${pedLibPath}/
#cp -rpf ../lib/aarch32/libcrypto.so ${pedLibPath}/
# cp -rpf ../lib/aarch32/libsqlite3.so ${pedLibPath}/
#module
# cp -rfp ../build/lib/libmodbus.so.1.0.0 ${pedModulePath}/
# cp -rfp ../build/lib/libdlt645.so.1.0.0 ${pedModulePath}/
# cp -rfp ../build/lib/libdlt698.so.1.0.0 ${pedModulePath}/

cp -rpf ../Package/settings.conf ${confPath}/

cp -rpf ../Package/bc ${binPath}/

cp -rpf ../Package/ped.conf ${ldconfigFilePath}/
cp -rpf ../package/ped.service ${servicePath}/
cp -rpf ../package/upgrade.service ${servicePath}/


echo "begin to create installation package..."
# chmod 777 ../Package/installHost.sh
chmod 777 ../Package/uninstall.sh
chmod 777 ../Package/install.sh
# chmod 777 ../Package/load_config.sh
# chmod 777 ../Package/docker_load_config.sh
# chmod 777 ../Package/upgrade.sh
cp -rpf ../Package/install.sh ${rootPath}/install.sh
cp -rpf ../Package/uninstall.sh ${rootPath}/uninstall.sh
# cp -rpf ../Package/uninstall.sh ${rootPath}/
# cp -rpf ../Package/load_config.sh ${pedDataPath}
# cp -rpf ../Package/docker_load_config.sh ${pedDataPath}
# cp -rpf ../Package/upgrade.sh ${upgradeBinPath}/

echo "create tar file ..."
tar -zcvf ../install/ped_$2_$1.tar.gz -C"../install/$1/" ped/  install.sh uninstall.sh
rm -rf ../install/$1
echo "create intallation package finished."
